home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / RWHO / clilib.c next >
C/C++ Source or Header  |  1991-07-11  |  3KB  |  177 lines

  1. /*
  2.     Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
  3. */
  4.  
  5. #ifndef    lint
  6. static    char    RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RWHO/RCS/clilib.c,v 1.1 91/06/17 14:39:26 mjr Rel $";
  7. #endif
  8.  
  9. /*
  10. code to interface client MUDs with rwho server
  11.  
  12. this is a standalone library.
  13. */
  14.  
  15.  
  16. #include    <stdio.h>
  17. #include    <ctype.h>
  18. #include    <fcntl.h>
  19. #include    <sys/file.h>
  20. #include    <sys/time.h>
  21. #include    <sys/types.h>
  22. #include    <sys/socket.h>
  23. #include    <netinet/in.h>
  24. #include    <netdb.h>
  25.  
  26.  
  27. #define    DGRAMPORT        6888
  28.  
  29. #ifndef    NO_HUGE_RESOLVER_CODE
  30. extern    struct    hostent    *gethostbyname();
  31. #endif
  32.  
  33. extern    char    *malloc();
  34.  
  35.  
  36. static    int    dgramfd = -1;
  37. static    char    *password;
  38. static    char    *localnam;
  39. static    char    *lcomment;
  40. static    struct    sockaddr_in    addr;
  41. time_t    senttime;
  42.  
  43.  
  44. /* enable RWHO and send the server a "we are up" message */
  45. rwhocli_setup(server,serverpw,myname,comment)
  46. char    *server;
  47. char    *serverpw;
  48. char    *myname;
  49. char    *comment;
  50. {
  51. #ifndef    NO_HUGE_RESOLVER_CODE
  52.     struct    hostent        *hp;
  53. #endif
  54.     char            pbuf[512];
  55.     char            *p;
  56.  
  57.     if(dgramfd != -1)
  58.         return(1);
  59.  
  60.     password = malloc(strlen(serverpw) + 1);
  61.     localnam = malloc(strlen(myname) + 1);
  62.     lcomment = malloc(strlen(comment) + 1);
  63.     if(password == (char *)0 || localnam == (char *)0 || lcomment == (char *)0)
  64.         return(1);
  65.     strcpy(password,serverpw);
  66.     strcpy(localnam,myname);
  67.     strcpy(lcomment,comment);
  68.  
  69.     p = server;
  70.     while(*p != '\0' && (*p == '.' || isdigit(*p)))
  71.         p++;
  72.  
  73.     if(*p != '\0') {
  74. #ifndef    NO_HUGE_RESOLVER_CODE
  75.         if((hp = gethostbyname(server)) == (struct hostent *)0)
  76.             return(1);
  77.         (void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
  78. #else
  79.         return(1);
  80. #endif
  81.     } else {
  82.         unsigned long    f;
  83.  
  84.         if((f = inet_addr(server)) == -1L)
  85.             return(1);
  86.         (void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
  87.     }
  88.  
  89.     addr.sin_port = htons(DGRAMPORT);
  90.     addr.sin_family = AF_INET;
  91.  
  92.     if((dgramfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
  93.         return(1);
  94.  
  95.     time(&senttime);
  96.  
  97.     sprintf(pbuf,"U\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
  98.         localnam,password,localnam,senttime,comment);
  99.     sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  100.     return(0);
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. /* disable RWHO */
  108. rwhocli_shutdown()
  109. {
  110.     char    pbuf[512];
  111.  
  112.     if(dgramfd != -1) {
  113.         sprintf(pbuf,"D\t%.20s\t%.20s\t%.20s",localnam,password,localnam);
  114.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  115.         close(dgramfd);
  116.         dgramfd = -1;
  117.         free(password);
  118.         free(localnam);
  119.     }
  120.     return(0);
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /* send an update ping that we're alive */
  128. rwhocli_pingalive()
  129. {
  130.     char    pbuf[512];
  131.  
  132.     if(dgramfd != -1) {
  133.         sprintf(pbuf,"M\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
  134.             localnam,password,localnam,senttime,lcomment);
  135.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  136.     }
  137.     return(0);
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. /* send a "so-and-so-logged in" message */
  145. rwhocli_userlogin(uid,name,tim)
  146. char    *uid;
  147. char    *name;
  148. time_t    tim;
  149. {
  150.     char    pbuf[512];
  151.  
  152.     if(dgramfd != -1) {
  153.         sprintf(pbuf,"A\t%.20s\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.20s",
  154.             localnam,password,localnam,uid,tim,name);
  155.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  156.     }
  157.     return(0);
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164. /* send a "so-and-so-logged out" message */
  165. rwhocli_userlogout(uid)
  166. char    *uid;
  167. {
  168.     char    pbuf[512];
  169.  
  170.     if(dgramfd != -1) {
  171.         sprintf(pbuf,"Z\t%.20s\t%.20s\t%.20s\t%.20s",
  172.             localnam,password,localnam,uid);
  173.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  174.     }
  175.     return(0);
  176. }
  177.